/**
 * Created by dell on 17-1-3.
 */
$(document).ready(function(){
    JudgeBrowser.changeTopBarTextSizeInIE();
    if (JudgeLogin.isLogin()) {
        if (window.localStorage.selectStaff == undefined || window.localStorage.selectStaff == "undefined") {
            alert('请选择修改的员工');
            window.location.href = "../../html/home/staffList.html";
        } else  {
            var selectStaff = JSON.parse(window.localStorage.selectStaff);
            setupManagerName();
            getLocalStaffInfo(selectStaff);
            InputCheck.checkInputValueIsNumber("#handPhone");
            bindEvent(selectStaff);
        }
    }
});

function bindEvent(selectStaff) {
    callChangeService(selectStaff);
    gotoStaffListInterface();
    gotoCompanyInfoInterface();
}

/* 设置管理员 */
function setupManagerName() {
    Api.fetchCompanyDetailInfo(function (result) {
        $(".managerBaseInfo").text("管理员：" + result.data.companyName);
        $("#companyName").val(result.data.companyName);
    }, function (error) {
        alert("获取管理员名字失败");
    })
}

/* 读取本地存储的员工信息 */
function getLocalStaffInfo(selectStaff){
    console.log(selectStaff);
    selectStaff.sex = getStaffSexString(selectStaff.sex);
    $(".selectStaffSex").val(selectStaff.sex);
    setupStaffInfo(selectStaff);

}

/* 根据接口给出的性别返回需要展示的性别 */
function getStaffSexString(sex) {
    if (sex == "MALE") {
        return "男";
    } else if (sex == "FEMALE") {
        return "女";
    } else {
        return "未知";
    }
}

/* 点击企业信息 */
function gotoCompanyInfoInterface() {
    $(".company_info").click(function () {
        window.location.href = "../../html/company/companyInfo.html";
    })
}
/* 点击修改按钮 */
function callChangeService(selectStaff) {
    $(".addButton").click(function () {
        if (checkInputIsValid()) {
            var staffValue = getChangeStaffInfo();
            Api.changeStaffInfo(selectStaff.id, staffValue, function (result) {
                console.log(result);
                if (result.code == 200) {
                    alert("修改成功!");
                    window.localStorage.selectStaff = undefined;
                    window.location.href = "../../html/home/staffList.html";
                }
            }, function (error) {
                console.log(error);
            })
        }
    })
}

/* 点击返回 */
function gotoStaffListInterface() {
    $(".goBack").click(function () {
        window.location.href='../../html/home/staffList.html';
    });

}


/* 检查所有必填的input框 */
function checkInputIsValid() {
    return (
        checkInputIsNull($("#name").val() == "", "请输入员工姓名")
        && checkInputIsNull($("#verifyId").val() == "", "请输入员工身份证号")
        && checkInputIsNull($("#handlePhone").val() == "", "请输入员工手机号")
    );


}

/* 检查input框是否为空 */
function checkInputIsNull(condition, prompt) {
    if (condition) {
        alert(prompt);
        return false;
    }
    return true;
}

/* 获取输入的员工信息 */
function getChangeStaffInfo() {
    return {
        "name": $("#name").val(),
        "idNo": $("#verifyId").val(),
        "phone": $("#handPhone").val(),
        "department": $("#department").val(),
        "position": $("#location").val(),
        "doorCardNum": $("#number").val(),
        "sex": getPostStaffSexStr()
    }
}

/* 根据选取的性别获取接口需要的性别数据 */
function getPostStaffSexStr() {

    var selectString = $(".selectStaffSex").val();
    return {
        男: 'MALE',
        女: 'FEMALE',
        未知: 'UNKNOWN'
    }[selectString];

}

/* 初始化页面展示的员工信息 */
function setupStaffInfo(selectStaff) {
    console.log(selectStaff.phone);

    $("#name").val(selectStaff.name);
    $("#handPhone").val(selectStaff.phone);
    $("#verifyId").val(selectStaff.idNo);
    $("#department").val(selectStaff.department);
    $("#location").val(selectStaff.position);
    $("#number").val(selectStaff.doorCardNum);
}


